using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataMapper;

using log4net;

using GymManager.Exception;
using GymManager.Persistence.VO;

namespace GymManager.Persistence.DAO
{
    /// <summary>
    /// Class responsible by getting information from database about modality schedule
    /// </summary>
    public class StudentModalityScheduleDAO
    {
        // logger instance
        private ILog _logManager = LogManager.GetLogger(typeof(StudentModalityScheduleDAO));

        /// <summary>
        /// Method responsible by getting the number of students in according to a proposed modality
        /// schedule
        /// </summary>
        /// <param name="Int32">modality identifier</param>
        /// <param name="Int32">day of week identifier</param>
        /// <param name="String">time information (hh:mm)</param>
        /// <returns>Number of students</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public Int32 getNumberOfStudentsInClass(Int32 modalityId, Int32 dayOfWeek, String time)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            Int32 numberOfStudentsInClass = 0;

            try
            {
                // building query parameters
                IDictionary<String, Object> filterDictionary = new Dictionary<String, Object>();
                filterDictionary.Add("ModalityId", modalityId); filterDictionary.Add("DayOfWeek", dayOfWeek); filterDictionary.Add("Time", time);

                ISqlMapper ibatisMapper = DAOManager.getInstance();
                numberOfStudentsInClass = ibatisMapper.QueryForObject<Int32>("StudentModalitySchedule.getNumberOfStudentsInClass", filterDictionary);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving modality schedule information", ex);
                throw new PersistenceException("Error while retrieving modality schedule information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return numberOfStudentsInClass;
        }

        /// <summary>
        /// Method responsible by getting all schedule beloging to an specific student modality
        /// </summary>
        /// <param name="Int32">student modality identifier</param>
        /// <returns>IList - list of schedule belonging to an specific student modality</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public IList<StudentModalitySchedule> findByStudentModality(Int32 studentModalityId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // list to be returned
            IList<StudentModalitySchedule> studentModalityScheduleList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                studentModalityScheduleList = ibatisMapper.QueryForList<StudentModalitySchedule>("StudentModalitySchedule.findByStudentModality", studentModalityId);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving student modality schedule information", ex);
                throw new PersistenceException("Error while retrieving student modality schedule information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return studentModalityScheduleList;
        }

        /// <summary>
        /// This method is responsible by returning all students which have classes in a modality
        /// with an associated schedule
        /// </summary>
        /// <param name="modalityId"> modality identifier</param>
        /// <returns>
        ///     Schedule list where each element is a hashtable with the following structure:
        ///          DAY_OF_WEEK - Integer
        ///          TIME - String
        ///          STUDENT - String
        /// </returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public IList<Hashtable> getStudentScheduleByModality(Int32 modalityId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            IList<Hashtable> studentList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                studentList = ibatisMapper.QueryForList<Hashtable>("StudentModalitySchedule.getStudentsByClass", modalityId);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving modality schedule information", ex);
                throw new PersistenceException("Error while retrieving modality schedule information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return studentList;
        }
    }
}
